<?php
/**
 * Created by PhpStorm.
 * User: C。
 * Date: 2019/8/13
 * Time: 9:04
 */
namespace app\home\controller;

use app\common\model\CompanyVow as CompanyVowModel;
use app\common\model\CompanyGongdengOrder as CompanyGongdengOrderModel;

/**
 * 许愿控制器
 * Class Companyvow
 * @package app\home\controller
 */
class Companyvow extends Base
{
    /**
     * [index  明细列表]
     * @return json
     * @author [C。]
     */
    public function index()
    {
        $param = input('param.');
        $model = new CompanyGongdengOrderModel;
        $vmodel = new CompanyVowModel;
        $this->getPageAndSize($param);
        $user = $this->user;
        $map = [];
        try{
            $map['c.comid'] = $param['comid'];
            $map['c.type'] = 7;
            $field = 'c.id,c.gongdeng,c.day,c.content,c.total_price,c.paytime,c.status,c.states,m.img,m.username,m.tel mtel,m.sex,c.vid';
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getList($map, $this->from, $this->size,$field);
            foreach($lists as &$v){
                $http = substr($v['img'],0,4);
                if($http != 'http'){
                    $v['img'] = config('index.domain_name').$v['img'];
                }
                if(!empty($v['paytime'])){
                    $v['endtime'] = date('Y-m-d',$v['paytime'] + $v['day']*24*60*60);
                    $v['paytime'] = date('Y-m-d',$v['paytime']);
                }
                $vow = $vmodel->findData($v['vid'],'id','content,create_time');
                if(!empty($vow)){
                    $v['vcontent'] = $vow['content'];
                    $v['vtime'] = $vow['create_time'];
                }
                $huanyuan = $model->findOne(['type'=>6,'status'=>1,'vid'=>$v['vid']],'gongdeng,paytime');
                if(!empty($huanyuan)){
                    $v['huanyuan'] = $huanyuan['gongdeng'];
                    $v['is_huan'] = 1;
                    $v['huanyuan_time'] = date('Y-m-s H:i:s',$huanyuan['paytime']);
                }else{
                    $v['is_huan'] = 0;
                }
            }
        }catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
}